<?php
//define('FPDF_FONTPATH', 'C:\wamp\www\dnevnik\web\font\\');
define('FPDF_FONTPATH', '/home/dnevnik/public_html/font/');
class Stampa
{
    public function Stampa()
    {
    }

    public function tromesecje($razred_id)
    {
        $pdf = new FPDF('P', 'pt');
        $pdf->SetMargins(0,0,0);
        $pdf->AddFont('Arial', '', 'arial_normal.php');
        $pdf->SetFont('Arial', '', 14);

        $razred = DbFinder::from('Razred')->findPk($razred_id);
        $jezik = DbFinder::from('Predmet')->findPk($razred->getJezik())->getPredmetKratki();
        $razredni = DbFinder::from('Profil')->join('Profesor')->where('Profesor.Id', $razred->getProfesorId())->findOne();
        $uceniks = UcenikPeer::getJoin()->where('Ucenik.RazredId', $razred_id)->find();
        $opr_dbs = DbFinder::from('Izostanak')->select('UcenikId', 'Sum')->withColumn('count(Id)', 'Sum')->where('RazredId', $razred_id)->where('Opravdan', '1')->groupBy('UcenikId')->find();
        $neopr_dbs = DbFinder::from('Izostanak')->select(array('UcenikId', 'Sum'))->withColumn('count(Id)', 'Sum')->where('RazredId', $razred_id)->where('Opravdan', '0')->groupBy('UcenikId')->find();
        $oprs = array();
        foreach($opr_dbs as $opr_db)
          $oprs[$opr_db['UcenikId']] = $opr_db['Sum'];
        $neoprs = array();
        foreach($neopr_dbs as $neopr_db)
          $neoprs[$neopr_db['UcenikId']] = $neopr_db['Sum'];

        foreach($uceniks as $ucenik)
        {
           $dnevnik = new Dnevnik($ucenik);
           $predmets = $dnevnik->getPredmetiAll();
           $predmets[$razred->getJezik()] = $jezik;
           $ocenas = array();
           $ocena_dbs = DbFinder::from('Ocena')->where('UcenikId', $ucenik->getId())->where('CreatedAt', '>', '2010-02-01 00:00:00')->find();
           foreach($ocena_dbs as $ocena_db)
             $ocenas[$ocena_db->getPredmetId()][] = $ocena_db->getOcena();

           $pdf->AddPage();
           $koordinate = 40;
           $pdf->Text(20, $koordinate, iconv('UTF-8', 'Windows-1250', $ucenik->joinIme()));
           $koordinate += 30;
           foreach($predmets as $id => $naziv)
           {
              $koordinate += 20;
              $y = 130;
              $pdf->Text(20, $koordinate, iconv('UTF-8', 'Windows-1250', $naziv));
              if(isset($ocenas[$id]))
              foreach($ocenas[$id] as $ocena)
              {
                 $pdf->Text($y, $koordinate, iconv('UTF-8', 'Windows-1250', $ocena));
                 $y += 10;
              }
           }
           if(isset($oprs[$ucenik->getId()])) $pdf->Text(20, $koordinate + 40, iconv('UTF-8', 'Windows-1250', 'Ukupno opravdanih: '.$oprs[$ucenik->getId()]));
           if(isset($neoprs[$ucenik->getId()])) $pdf->Text(20, $koordinate + 60, iconv('UTF-8', 'Windows-1250', 'Ukupno neopravdanih: '.$neoprs[$ucenik->getId()]));
           $pdf->Text(20, $koordinate + 100, iconv('UTF-8', 'Windows-1250', 'Odeljenski starešina: '.$razredni->getPrezime().' '.$razredni->getIme()));
        }

        /*$profils = DbFinder::from('Profil')->join('Profesor')->where('Profesor.SkolaId', $this->skola_id)->orderBy('Profil.Prezime')->orderBy('Profil.Ime')->find();

        $brojac = 0;
        foreach($profils as $profil)
        {
           if(($brojac % 5) == 0)
           {
              $koordinate = 20;
              $pdf->AddPage();
           }

              $pdf->Text(20, $koordinate, iconv('UTF-8', 'Windows-1250', (($profil->getPol() == ProfilPeer::$pol['m']) ? 'Poštovani ' : 'Poštovana ').$profil->getPrezime().' '.$profil->getIme().'!'));
              $pdf->Text(20, ($koordinate + 11), iconv('UTF-8', 'Windows-1250', 'Zahvaljujemo Vam što koristite ONLINE sistem za evidenciju đačkih ocena za  pomoć roditeljima i učenicima.'));
              $pdf->Text(20, ($koordinate + 22), iconv('UTF-8', 'Windows-1250', 'Na stranicu www.skolskidnevnik.rs možete se ulogovati koristeći sledeće podatke:'));
              $pdf->Text(20, ($koordinate + 38), iconv('UTF-8', 'Windows-1250', 'Korisničko ime: '.$profil->getUsername()));
              $pdf->Text(20, ($koordinate + 49), iconv('UTF-8', 'Windows-1250', 'Šifra: '.$profil->getPlainPassword()));
              $pdf->Text(20, ($koordinate + 65), iconv('UTF-8', 'Windows-1250', 'E-mail korisnički podaci:'));
              $pdf->Text(20, ($koordinate + 76), iconv('UTF-8', 'Windows-1250', 'Korisničko ime: '.$profil->getEMail()));
              $pdf->Text(20, ($koordinate + 87), iconv('UTF-8', 'Windows-1250', 'Šifra: '.$profil->getPlainPassword()));
              $pdf->Text(20, ($koordinate + 103), iconv('UTF-8', 'Windows-1250', 'Za sve eventualne nejasnoće prilikom korišćenja programa možete nas  kontaktirati na tel: '.$this->tel));
              $pdf->Text(20, ($koordinate + 114), iconv('UTF-8', 'Windows-1250', 'ili putem korisničkog dela (link PREDLOZI/GREŠKE) ili e-mailom podrska@skolskidnevnik.rs'));
              $pdf->Text(20, ($koordinate + 129), iconv('UTF-8', 'Windows-1250', 'Elektronski školski dnevnik'));

              if(($brojac % 5) != 4)
                 $pdf->Line(20, ($koordinate + 144), 600,  ($koordinate + 144));

              $koordinate += 159;
              $brojac ++;
        }*/

         return $pdf;
    }

}